"use strict";
/* Controllers */
app.controller("contentAddController", ['$scope', '$location', '$rootScope', '$routeParams', 'contentAddService', function ($scope, $location, $rootScope, $routeParams, contentAddService) {

    var vm = $scope.vm = {};
    vm.p = {};
    vm.result = {};
    vm.p.depName = 'HRG';

    vm.p.type = $routeParams.type;
    if (vm.p.type == 'id') {
        vm.p.id = $routeParams.id;
    } else {
        vm.p.topicTbl = $routeParams.id;
    }

    var errorFn = function (result) {
        alert(result);
    };

    $scope.syncDpMeta = function() {
        var vm = $scope.vm;
        var tblName = vm.p.topicTbl;
        if(tblName=='') {
            alert('请输入表名称');
            return;
        }
        alert(tblName + ' sync ...');
//        vm.p.topicTblId
//        vm.p.topicTblDesc
//        vm.p.sourceRelation
//        vm.p.ownerId
    }
    $scope.getMetricSource = function () {
        var vm = $scope.vm;
        vm.result.ms = "...";
        // 请求参数
        var params = {
            id: vm.p.id,
            topicTbl: vm.p.topicTbl
        };

        // 结果处理
        var procFn = function (response) {
            if ("000" !== response.code) {
                alert(response.message);
                return ;
            }
            vm.result.ms = response.ms;
            var data = response.data;
            if (data == null) return;
            vm.result.data = data;
            vm.p = data;
            var sourceRelationFmt = function(list) {
                var ret = [];
                for(var i in list) {
                    var it = list[i];
                    if(it.relationType=='上游依赖') {
                        ret.push(it.topicTbl);
                    }
                }
                return ret.join(',');
            };
            vm.p.sourceRelation = sourceRelationFmt(data.relationEntityList);
        };
        contentAddService.getMetricSource(params, procFn, errorFn);
    };

    $scope.setMetricSource = function () {
        var vm = $scope.vm;
        var params = vm.p;
        var procFn = function (response) {
            if (response.success && response.data) {
                alert("执行成功！");
            } else {
                alert(response.msg);
            }
        };
        contentAddService.setMetricSource(params, procFn, errorFn);
    };

    $scope.initializeController = function () {
        vm.result.ms = '';
        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
             pageItem: [30, 50, 100, 300, 500, 1000], pageSize: 30
        });
        $scope.getMetricSource();
    };

    $scope.initializeController();

}]);


/* Services */
app.service('contentAddService', ['ajaxService', function (ajaxService) {

    this.getMetricSource = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/data_content/getMetricSource.do", successFunction, errorFunction);
    };

    this.setMetricSource = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/data_content/setMetricSource.do", successFunction, errorFunction);
    };
}]);


